package com.admin.control;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import com.admin.common.Show;
import com.admin.entity.feedbackData;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

public class feedbackListViewControl implements Initializable {
    @FXML
    private TableView<feedbackData> feedbackTableView;
    @FXML
    private TableColumn<feedbackData, String> idColumn;
    @FXML
    private TableColumn<feedbackData, String> nameColumn;
    @FXML
    private TableColumn<feedbackData, String> dateColumn;
    @FXML
    private TableColumn<feedbackData, String> timeColumn;
    @FXML
    private TableColumn<feedbackData, String> gradeColumn;
    @FXML
    private TableColumn<feedbackData, String> provinceColumn;
    @FXML
    private TableColumn<feedbackData, String> cityColumn;
    @FXML
    private TableColumn<feedbackData, Void> contentColumn;
    @FXML
    private TableColumn<feedbackData, Void> actionColumn;
    @FXML
    private ComboBox<String> provinceComboBox;
    @FXML
    private ComboBox<String> cityComboBox;
    @FXML
    private ComboBox<String> gradeComboBox;
    @FXML
    private ObservableList<feedbackData> feedbackDataList = FXCollections.observableArrayList();
    @FXML
    private Button clearButton;
    @FXML
    private DatePicker datePicker;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        ObservableList<String> provinceData = FXCollections.observableArrayList("全部", "北京", "上海", "广东");
        provinceComboBox.setItems(provinceData);

        // 填充市区域数据
        ObservableList<String> cityData = FXCollections.observableArrayList("请选择省区域");
        cityComboBox.setItems(cityData);

        // 填充预估等级数据
        ObservableList<String> gradeData = FXCollections.observableArrayList("全部", "A", "B", "C");
        gradeComboBox.setItems(gradeData);

        // 为省区域ComboBox添加选择事件处理
        provinceComboBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue != null) {
                System.out.println("选择的省区域: " + newValue);
                // 根据选择的省区域更新市区域ComboBox的内容
                if ("广东".equals(newValue)) {
                    cityComboBox.setItems(FXCollections.observableArrayList("全部", "广州", "深圳", "珠海"));
                } else if ("北京".equals(newValue)) {
                    cityComboBox.setItems(FXCollections.observableArrayList("全部", "北京"));
                } else if ("上海".equals(newValue)) {
                    cityComboBox.setItems(FXCollections.observableArrayList("全部", "上海"));
                }
                handleSearch();
            }
        });
        // 为市区域ComboBox添加选择事件处理
        cityComboBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue != null) {
                System.out.println("选择的市区域: " + newValue);
                handleSearch();
            }
        });
        // 为预估等级ComboBox添加选择事件处理
        gradeComboBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue != null) {
                System.out.println("选择的预估等级: " + newValue);
                handleSearch();
            }
        });
        // 为清空筛选按钮添加事件处理
        clearButton.setOnAction(event -> {
            provinceComboBox.getSelectionModel().clearSelection();
            cityComboBox.getSelectionModel().clearSelection();
            gradeComboBox.getSelectionModel().clearSelection();
            datePicker.setValue(null);
            handleSearch();
        });
        // 为日期选择器添加选择事件处理
        datePicker.setOnAction(event -> {
            handleSearch();
        });
        // 创建一些示例数据
        feedbackData fdData1 = new feedbackData("1", "张三", "北京", "北京", "A", "2023-12-01", "10:00");
        feedbackData fdData2 = new feedbackData("2", "李四", "上海", "上海", "B", "2023-12-02", "11:00");
        feedbackData fdData3 = new feedbackData("3", "王五", "广东", "广州", "C", "2023-12-03", "12:00");
        feedbackData fdData4 = new feedbackData("4", "赵六", "北京", "北京", "A", "2023-12-04", "13:00");
        // 将数据添加到 TableView 中
        feedbackDataList.addAll(fdData1, fdData2, fdData3, fdData4);

        // 绑定列与属性
        idColumn.setCellValueFactory(new PropertyValueFactory<>("id"));
        nameColumn.setCellValueFactory(new PropertyValueFactory<>("name"));
        dateColumn.setCellValueFactory(new PropertyValueFactory<>("date"));
        timeColumn.setCellValueFactory(new PropertyValueFactory<>("time"));
        gradeColumn.setCellValueFactory(new PropertyValueFactory<>("grade"));
        provinceColumn.setCellValueFactory(new PropertyValueFactory<>("province"));
        cityColumn.setCellValueFactory(new PropertyValueFactory<>("city"));
        // 将数据设置到 TableView 中
        feedbackTableView.setItems(feedbackDataList);
        // 设置内容列的单元格工厂
        contentColumn.setCellFactory(cols -> new TableCell<>() {
            private Button btn = new Button("查看内容");
            {
                btn.setOnAction(_ -> {
                    feedbackData data = getTableView().getItems().get(getIndex());
                    System.out.println("查看内容: " + data.getId());
                    // 在这里可以添加查看内容的逻辑，比如弹出一个新窗口显示反馈内容
                    try {
                        Stage stage = new Stage();
                        stage.initStyle(StageStyle.UNDECORATED);
                        URL url = getClass().getResource("/com/admin/view/feedbackView.fxml");
                        Show.show(stage, url, "反馈详情");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });
            }

            @Override
            protected void updateItem(Void item, boolean empty) {
                super.updateItem(item, empty);
                // 根据单元格是否为空设置显示的图形，空则不显示按钮
                setGraphic(empty ? null : btn);
            }
        });
        actionColumn.setCellFactory(cols -> new TableCell<>() {
            private Button btn = new Button("处理");
            {
                btn.setOnAction(_ -> {
                    feedbackData data = getTableView().getItems().get(getIndex());
                    System.out.println("处理反馈: " + data.getId());
                    // 在这里可以添加处理反馈的逻辑，比如弹出一个确认对话框
                    try {
                        Stage stage = new Stage();
                        // 设置舞台样式为无边框
                        stage.initStyle(StageStyle.UNDECORATED);
                        URL url = getClass().getResource("/com/admin/view/feedbackView.fxml");
                        stage.setTitle("处理反馈");
                        // 这里可以传递数据到新窗口
                        Show.show(stage, url, "反馈内容查看");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });
            }

            @Override
            protected void updateItem(Void item, boolean empty) {
                super.updateItem(item, empty);
                // 根据单元格是否为空设置显示的图形，空则不显示按钮
                setGraphic(empty ? null : btn);
            }
        });
    }

    public void handleSearch() {
        // 获取用户选择的筛选条件
        String selectedProvince = provinceComboBox.getValue();
        String selectedCity = cityComboBox.getValue();
        String selectedGrade = gradeComboBox.getValue();
        String selectedDate = datePicker.getValue() != null ? datePicker.getValue().toString() : null;
        // 创建一个新的列表来存储筛选后的数据
        ObservableList<feedbackData> filteredList = FXCollections.observableArrayList();

        // 遍历原始数据列表
        for (feedbackData data : feedbackDataList) {
            boolean matchProvince = true;
            boolean matchCity = true;
            boolean matchGrade = true;
            boolean matchDate = true;
            // 检查日期条件
            if (selectedDate != null && !selectedDate.isEmpty() && !selectedDate.equals("全部")) {
                matchDate = data.getDate().equals(selectedDate);
            } else {
                matchDate = true; // 如果没有选择日期，则不进行筛选
            }
            // 检查省份条件
            if (selectedProvince != null && !selectedProvince.isEmpty() && !selectedProvince.equals("全部")) {
                matchProvince = data.getProvince().equals(selectedProvince);
            }

            // 检查城市条件
            if (selectedCity != null && !selectedCity.isEmpty() && !selectedCity.equals("请选择省区域")
                    && !selectedCity.equals("全部")) {
                matchCity = data.getCity().equals(selectedCity);
            }

            // 检查等级条件
            if (selectedGrade != null && !selectedGrade.isEmpty() && !selectedGrade.equals("全部")) {
                matchGrade = data.getGrade().equals(selectedGrade);
            }

            // 如果所有条件都匹配，则将数据添加到筛选列表中
            if (matchProvince && matchCity && matchGrade && matchDate) {
                filteredList.add(data);
            }
        }

        // 将筛选后的数据设置到 TableView 中
        feedbackTableView.setItems(filteredList);

        // 打印搜索条件
        System.out.println("搜索条件: 省区域=" + selectedProvince + ", 市区域=" + selectedCity + ", 预估等级=" + selectedGrade
                + ", 日期=" + selectedDate);
    }
}
